﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MBS_FoodBooking.Models;

using System.Xml.Linq;
using System.Web.Services;

namespace MBS_FoodBooking.Controllers
{
    public class FoodBookingController : Controller
    {
        //
        // GET: /FoodBooking/
        ShoppingCartModel cart = new ShoppingCartModel();
        FoodMenu menu = new FoodMenu();
        
        public ActionResult FoodMenu(int? id) 
        {
            if (id != null)
            {
                cart.BookingId = (int)id;
            }
            
            return View(menu);
        }

       
        public PartialViewResult AddtoCart(FormCollection formCollection)
        {
            string quantity = formCollection["txtQuantity"];
            string[] quantityList = quantity.Split(',');
            List<CartItem> lstCartItems = lstCartItems = new List<CartItem>();
            int j = 0;
            decimal totalPrice = 0.0M;
            for (int i = 0; i < menu.MenuItemList.Count; i++)
            {
                if (quantityList[i]!=string.Empty)
                {
                    CartItem cartItem = new CartItem();
                    cartItem.Id = j++;
                    cartItem.ImgUrl = menu.MenuItemList[i].ImgUrl;
                    cartItem.ItemName = menu.MenuItemList[i].Name;
                    cartItem.ItemPrice = menu.MenuItemList[i].ItemPrice;
                    cartItem.Quantity = Convert.ToInt32(quantityList[i]);
                    totalPrice += (menu.MenuItemList[i].ItemPrice * Convert.ToInt32(quantityList[i]));
                    lstCartItems.Add(cartItem); 
                }
            
            }

            
            cart.LstShoppingCart = lstCartItems;
            cart.Price = totalPrice;
            cart.BookingId = 5;//Convert.ToInt32(Session["BookingId"]);
            Session["shoppingCartList"] = cart;
            menu.LstShoppingCart = cart;
            return PartialView("~/Views/Shared/Controls/ShoppingCartControl.ascx", cart);
          
        }


        public PartialViewResult RemovefromCart(FormCollection form)
        {
           
            int i = Convert.ToInt32(form[0]);
            cart = (ShoppingCartModel)Session["shoppingCartList"];
            List<CartItem> lstCartItems = cart.LstShoppingCart;
            CartItem item = lstCartItems[i];
            lstCartItems.RemoveAt(i);

            cart.Price -= (item.ItemPrice * item.Quantity);
            cart.LstShoppingCart = lstCartItems;
            Session["shoppingCartList"] = cart;
            menu.LstShoppingCart = cart;
            return PartialView("~/Views/Shared/Controls/ShoppingCartControl.ascx", cart);
            
        }

        
        public ActionResult ShowCart(FormCollection frmCollection)
        {
            cart.LstShoppingCart = (List<CartItem>)Session["shoppingCartList"];
            return View("ViewCart", cart);
        }

        public ActionResult RedirectToMBSHome()
        {
            return Redirect("http://localhost:49286/About.aspx");
        }
    }
}
